<template>
  <view class="content">
    <van-nav-bar title="消费记录" left-text="" :left-arrow="true" placeholder fixed @click-left="onClickLeft" />
    <view class="expense">
      <Scrollbox @lowerFun="getList" :status="loadingStatus">
        <template #contScrollBox>
          <view class="expense-box">
            <view class="expense-list" v-for="(item, index) in list" :key="index">
              <view class="title">
                <view class="title-name">{{ item.product_name }}</view>
                <text>已完成</text>
              </view>
              <view class="expense-info">
                <view class="info-list">
                  <view class="info-label">账单编号：</view>
                  <view class="info-cont">{{ item.order_code }}</view>
                </view>
                <view class="info-list">
                  <view class="info-label">创建时间：</view>
                  <view class="info-cont">{{ item.add_time }}</view>
                </view>
                <view class="info-list" v-for="(v, i) in item.discount" :key="i">
                  <view class="info-label">{{ v.discount_name }}抵扣：</view>
                  <view class="info-cont">{{ v.discount_price }}</view>
                </view>
              </view>
              <view class="price-box">
                <view class="price-text">{{ item.product_price }}</view>
                <view class="price-text">实付：{{ item.pay_price }}</view>
              </view>
              <view class="expense-option">
                <view class="option-btn" @click="goPage">联系客服</view>
              </view>
            </view>
          </view>
        </template>
      </Scrollbox>
    </view>
  </view>
</template>

<script>
import { reactive, toRefs } from 'vue'
import Scrollbox from '@/components/scrollbox/index'
import { onShow } from '@dcloudio/uni-app'
import { getOrders } from '@/http/buy'
export default {
  components: {
    Scrollbox,
  },
  setup() {
    const state = reactive({
      list: [],
      listQuery: {
        page: 1,
      },
      loadingStatus: 'loading',
      isEnd: false,
    })
    const onClickLeft = () => {
      uni.navigateBack({
        delta: 1,
      })
    }
    const getList = async () => {
      if (state.isEnd) return
      try {
        let data = await getOrders(state.listQuery)
        const { list, total_page } = data.data
        state.list = state.listQuery.page === 1 ? list : state.list.concat(list)

        if (list.length === 0 || total_page === state.listQuery.page) {
          state.loadingStatus = 'loadmore'
          state.isEnd = true
        } else {
          state.loadingStatus = 'loading'
          state.listQuery.page++
        }
      } catch (e) {
        state.isRefresh = false
        state.loading = false
      }
    }
    const goPage = () => {
      uni.navigateTo({
        url: `/subpackage-platform/customer/index`,
      })
    }
    onShow(() => {
      state.listQuery.page = 1
      state.isEnd = false
      getList()
    })
    return {
      ...toRefs(state),
      onClickLeft,
      getList,
      goPage,
    }
  },
}
</script>

<style lang="scss">
.content {
  height: 100vh;
}
.expense-tab {
  width: calc(100% - 48rpx);
  display: flex;
  justify-content: space-between;
  color: #6c7385;
  padding: 0 24rpx;
  background: #fff;
  font-size: 28rpx;
  text {
    position: relative;
    display: block;
    min-width: 112rpx;
    height: 96rpx;
    padding: 0 24rpx;
    line-height: 96rpx;
    &.active {
      font-weight: 500;
      color: #3775f2;
      &::before {
        content: '';
        display: block;
        height: 4rpx;
        width: calc(100% - 48rpx);
        background: #3775f2;
        position: absolute;
        bottom: 0;
        left: 24rpx;
      }
    }
  }
}
.expense-box {
  padding: 24rpx;
}
.expense-list {
  position: relative;
  width: calc(100% - 48rpx);
  margin-bottom: 24rpx;
  padding: 24rpx;
  border-radius: 16rpx;
  background: #fff;
  .title {
    height: 48rpx;
    line-height: 48rpx;
    margin-bottom: 24rpx;
    display: flex;
    justify-content: space-between;
    width: 100%;
    .title-name {
      max-width: calc(100% - 30rpx);
      @include text-ellipsis(1);
    }
    text {
      font-size: 28rpx;
      color: #7f8798;
    }
  }
  .expense-info {
    padding: 24rpx 0 16rpx;
    .info-list {
      display: flex;
      width: 100%;
      line-height: 40rpx;
      font-size: 28rpx;
      color: #7f8798;
      margin-bottom: 8rpx;
      .info-label {
        width: 224rpx;
      }
      .info-cont {
        width: calc(100% - 224rpx);
        color: #2b303b;
      }
    }
  }
  .expense-option {
    display: flex;
    justify-content: flex-end;
    .option-btn {
      height: 64rpx;
      line-height: 64rpx;
      font-size: 28rpx;
      color: #2b303b;
      padding: 0 32rpx;
      border-radius: 32rpx;
      border: 2rpx solid #e8e8e8;
    }
  }
}
.price-box {
  padding: 12rpx 0;
  .price-text {
    padding: 12rpx 0;
    text-align: right;
    &:nth-child(1) {
      border-bottom: 2rpx solid #ddd;
    }
  }
}
</style>
